package mahan.test.data.mongodb;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Arrays;

import mahan.data.model.Product;
import mahan.data.mongodb.MahanDB;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

public class ProductTestCase {
	private MahanDB mahanDb;

	@After
	public void clean() {
		mahanDb.get().dropDatabase();
	}

	@Before
	public void setup() {
		mahanDb = new MahanDB("localhost", 27017, "prod");
		DB db = mahanDb.get();
		assertNotNull(db);
	}

	@SuppressWarnings("unchecked")
	@Test
	public void testProduct() throws Exception {

		Float[] bigKidSizes = new Float[] { (float) 1.0, (float) 1.5,
				(float) 2.0, (float) 2.5 };

		clean();
		Product product = new Product("LAX", "Las Vegas", 111);
		product.setProductID(23);
		product.setBigKids(true);
		product.setBigKidSizes(Arrays.asList(bigKidSizes));

		product.setCostPrice(25.50);
		product.setDiscountPrice(4.25);

		product.setLittleKids(true);
		product.setLittleKidSizes(Arrays.asList(bigKidSizes));

		product.setMens(true);
		product.setMenSizes(Arrays.asList(bigKidSizes));

		product.setSellingPrice(100.34);
		product.setWomens(true);
		product.setWomenSizes(Arrays.asList(bigKidSizes));

		// Store into DB
		BasicDBObject dbo = new BasicDBObject();
		dbo.putAll(product.get());
		DBCollection coll = mahanDb.createCollection("prod");
		coll.save(dbo);

		// Retrieve from DB
		DBObject fromDB = coll.findOne();
		Product from = Product.instance();
		from.put(fromDB.toMap());

		assertEquals("LAX", from.getSku());
		assertEquals("Las Vegas", from.getBrandName());
		assertEquals(111, from.getVendorID());
		assertEquals(23, from.getProductID());
		assertEquals(true, from.isBigKids());
		assertEquals(true, from.isLittleKids());
		assertEquals(true, from.isMens());
		assertEquals(true, from.isWomens());
		assertEquals(4, from.getBigKidSizes().size());
		assertEquals(4, from.getLittleKidSizes().size());
		assertEquals(4, from.getMenSizes().size());
		assertEquals(4, from.getWomenSizes().size());
		assertEquals(25.50, from.getCostPrice(), 0.01);
		assertEquals(4.25, from.getDiscountPrice(), 0.01);
		assertEquals(100.34, from.getSellingPrice(), 0.01);
	}
}